package com.example.gulimallproduct.service.impl;

import com.example.common.utils.R;
import com.example.gulimallproduct.entity.BrandEntity;
import com.example.gulimallproduct.entity.CategoryEntity;
import com.example.gulimallproduct.service.BrandService;
import com.example.gulimallproduct.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.utils.PageUtils;
import com.example.common.utils.Query;

import com.example.gulimallproduct.dao.CategoryBrandRelationDao;
import com.example.gulimallproduct.entity.CategoryBrandRelationEntity;
import com.example.gulimallproduct.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {
    @Autowired
    private BrandService brandService;
    @Autowired
    private CategoryService categoryService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    @Override
    public R saveCategoryBrandRelation(CategoryBrandRelationEntity categoryBrandRelation) {
        Long count = lambdaQuery().eq(CategoryBrandRelationEntity::getBrandId, categoryBrandRelation.getBrandId())
                .eq(CategoryBrandRelationEntity::getCatelogId, categoryBrandRelation.getCatelogId())
                .count();
        if(count > 0){
            return R.error("分类品牌关联关系已存在");
        }
        //1. 检查品牌是否存在
        BrandEntity brandEntity = brandService.lambdaQuery()
                        .select(BrandEntity::getName)
                        .eq(BrandEntity::getBrandId, categoryBrandRelation.getBrandId())
                        .one();
        if(Objects.isNull(brandEntity)){
            return R.error("品牌不存在");
        }
        //2. 检查分类是否存在
        CategoryEntity categoryEntity = categoryService.lambdaQuery()
                        .select(CategoryEntity::getName)
                        .eq(CategoryEntity::getCatId, categoryBrandRelation.getCatelogId())
                        .one();
        if(Objects.isNull(categoryEntity)){
            return R.error("分类不存在");
        }
        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        //3. 保存关联关系
        this.save(categoryBrandRelation);
        return R.ok();
    }

    @Override
    public R catelogList(Long brandId) {
        List<CategoryBrandRelationEntity> categoryBrandRelationEntities = lambdaQuery()
                .eq(CategoryBrandRelationEntity::getBrandId, brandId)
                .list();
        return R.ok().put("data", categoryBrandRelationEntities);
    }

    @Override
    public R brandsList(Long catId) {
        List<CategoryBrandRelationEntity> list = lambdaQuery().eq(CategoryBrandRelationEntity::getCatelogId, catId).list();
        List<BrandEntity> brandEntities = brandService
                .lambdaQuery()
                .in(BrandEntity::getBrandId, list.stream().map(CategoryBrandRelationEntity::getBrandId).toList())
                .list();
        return R.ok().put("data", brandEntities);
    }

}